<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Expansion\Calendar;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CalendarService
{

    use Service;

    /**
     * 日历事件列表
     *
     * @return array
     */
    public function FullCalendarService()
    {
        $where  = function ( $query ) {
            $query->where('userId', Auth::id());
        };
        $select = [ 'id', 'title', 'startTime', 'endTime', 'allDay', 'color', 'content' ];
        $res    = Calendar::GetModel($where, $select);
        $data   = [];
        foreach ( $res as $val ) {
            $allDay = $val[ 'allDay' ] == 1 ? TRUE : FALSE;//三元判断
            $data[] = [
                'id'              => $val[ 'id' ],
                'title'           => $val[ 'title' ],//事件标题
                'start'           => $val[ 'startTime' ],//事件开始时间
                'end'             => $val[ 'endTime' ],//事件结束时间
                'allDay'          => $allDay,//是否全天时间
                'backgroundColor' => $val[ 'color' ],//颜色时间
                'content'         => $val[ 'content' ],//颜色时间
            ];
        }
        return $data;
    }

    /**
     * 日历添加事件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FullCalendarStoreService( $request )
    {
        $validator = $this->getAdminValidator()->FullCalendarStoreValidator($request);
        if ( $validator->first() ) return message($validator->first());
        $data[ 'title' ]          = $request->input('title');
        $data[ 'color' ]          = $request->input('color');
        $data[ 'startTime' ]      = $request->input('start');
        $data[ 'userID' ]         = Auth::id();//登陆id
        $res                      = Calendar::CreateModel($data);
        $res[ 'backgroundColor' ] = $res[ 'color' ];
        unset($res[ 'color' ]);
        errorStorage('calendar', '"创建事件" : ' . $request->input('title'), '创建事件成功', 'success', '创建事件');
        return data($res, '创建成功');
    }

    /**
     * 修改日历事件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FullCalendarUpdateService( $request )
    {
        $error          = $this->getErrorCode();
        $adminValidator = $this->getAdminValidator();
        switch ( $request->input('active') ) {
            case 'drop':
                $validator = $adminValidator->FullCalendarUpdateValidator($request);
                if ( $validator->first() ) return message($validator->first());
                $data[ 'startTime' ] = $request->input('start');
                if ( $request->filled('end') ) $data[ 'endTime' ] = $request->input('end');
                $where = function ( $query ) use ( $request ) {
                    $query->where('id', $request->input('id'));
                };
                Calendar::UpdateModel($where, $data);
                break;
            case 'edit':
                $validator = $adminValidator->FullCalendarUpdateValidator($request);
                if ( $validator->first() ) return message($validator->first());
                $date = $request->input('data');
                if ( $date[ 'allDay' ] != 1 ) {
                    $validator = $adminValidator->FullCalendarUpdateAllDayValidator($request);
                    if ( $validator->first() ) return message($validator->first());
                    $start = Carbon::parse($date[ 'start' ]);
                    $end   = Carbon::parse($date[ 'end' ]);
                    if ( ! $start->lt($end) ) return message($error::message(1020));
                    $data[ 'endTime' ] = $date[ 'end' ];
                    $data[ 'allDay' ]  = $date[ 'allDay' ];
                }
                else {
                    $data[ 'allDay' ]  = 1;
                    $data[ 'endTime' ] = NULL;
                }
                $data[ 'startTime' ] = $date[ 'start' ];
                $data[ 'color' ]     = $date[ 'color' ];
                $data[ 'title' ]     = $date[ 'title' ];
                if ( $date[ 'content' ] ) $data[ 'content' ] = $date[ 'content' ];
                $where = function ( $query ) use ( $date ) {
                    $query->where('id', $date[ 'id' ]);
                };
                Calendar::UpdateModel($where, $data);
                break;
            default:
                return message($error::message(1001));
                break;
        }
        errorStorage('calendar', '"修改事件" : ' . $request->input('title'), '修改事件成功', 'success', '修改事件');
        return data(NULL, $error::message(203));
    }

    /**
     * 删除日历事件
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function FullCalendarDestroyService( $request )
    {
        $error = $this->getErrorCode();
        if ( ! $request->filled('id') ) return message($error::message(1001));
        Calendar::DestroyModel($request->input('id'));
        errorStorage('calendar', '"删除事件" : ' . $request->input('title'), '删除事件成功', 'success', '删除事件');
        return data(NULL, $error::message(204));
    }
}
